import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution841 {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        Queue<Integer> queue=new LinkedList<>();
        int[] visit=new int[rooms.size()];
        visit[0]=1;
        int ans=1;
        queue.offer(0);
        while (!queue.isEmpty()){
            int q=queue.poll();
            List<Integer> list=rooms.get(q);
            for (Integer integer : list) {
                if (visit[integer]==0){
                    visit[integer]=1;
                    queue.offer(integer);
                    ans++;
                }
            }
        }
        return ans==rooms.size();
    }

    public static void main(String[] args) {
        List<List<Integer>> lists=new ArrayList<>();
        List<Integer> l1=new ArrayList<>();
        l1.add(1);lists.add(l1);
        List<Integer> l2=new ArrayList<>();
        l2.add(2);lists.add(l2);
        List<Integer> l3=new ArrayList<>();
        l3.add(3);lists.add(l3);
        lists.add(new ArrayList<>());
        System.out.println(new Solution841().canVisitAllRooms(lists));
    }
}
